<?php

/**
 * 服务器管理，管理服务器列表
 *
 */
class Server extends adminControl
{
	protected function indexAction()
	{
		$rs = $this->db->get_array("SELECT * FROM cms_server ORDER BY serverid DESC");
		$this->fillData('serverdb', $rs);
		$this->display('set_server');
	}

	protected function getAddAction()
	{
		$this->display('set_server');
	}

	protected function postAddAction()
	{
		$servername = Char_cv($_POST['servername']);
		$serverpath = Char_cv($_POST['serverpath']);
		$serverpath2 = Char_cv($_POST['serverpath2']);
		$single = intval($_POST['single']);
		$serverurl = $this->checkURL($_POST['serverurl']);

		if (empty($servername) || empty($serverpath) || empty($serverurl))
		{
			Showmsg('您填写的信息不完整，请返回完整填写');
		}

		$this->checkPath($serverpath, $serverurl);
		if (!is_dir($serverpath))
		{
			$serverpath = stripslashes($serverpath);
			Showmsg($serverpath . '不是一个可用的目录或者系统无权限访问该目录。');
		}

		$this->db->update("INSERT INTO cms_server SET
			`servername`='$servername',
			`serverpath`='$serverpath',
			`serverpath2`='$serverpath2',
			`serverurl`='$serverurl',
			`single`='$single'
		");
		require_once(R_P . 'require/class_cache.php');
		Cache::writeCache('server');
		adminmsg('您成功添加了一个存储服务器点');
	}

	protected function getEditAction()
	{
		$serverid = intval($_GET['serverid']);
		$rs = $this->db->get_one("SELECT * FROM cms_server WHERE serverid='$serverid'");

		if (empty($rs))
		{
			Showmsg('不存在的存储服务器数据');
		}
		$rs['single'] ? $single_Y = 'CHECKED' : $single_N = 'CHECKED';
		$this->fillData('single_Y', $single_Y);
		$this->fillData('single_N', $single_N);
		$this->fillData('edit', $rs);
		$this->display('set_server');
	}

	protected function postEditAction()
	{
		$serverid = intval($_POST['serverid']);

		if (empty($serverid)) Showmsg('数据错误，没有传递服务器ID');

		$servername = Char_cv($_POST['servername']);
		$serverpath = Char_cv($_POST['serverpath']);
		$serverurl = $this->checkURL($_POST['serverurl']);
		$serverpath2 = Char_cv($_POST['serverpath2']);
		$single = intval($_POST['single']);

		if (empty($servername) || empty($serverpath) || empty($serverurl))
		{
			Showmsg('您填写的信息不完整，请返回完整填写');
		}

		$this->checkPath($serverpath, $serverurl);
		if (!is_dir($serverpath))
		{
			$serverpath = stripslashes($serverpath);
			Showmsg($serverpath . '不是一个可用的目录或者系统无权限访问该目录。');
		}

		$this->db->update("UPDATE cms_server SET
			`servername`='$servername',
			`serverpath`='$serverpath',
			`serverpath2`='$serverpath2',
			`serverurl`='$serverurl',
			`single`='$single'
			WHERE serverid='$serverid'
		");

		require_once(R_P . 'require/class_cache.php');
		Cache::writeCache('server');
		adminmsg('您成功修改了一个存储服务器点');

	}

	/**
	 * 删除服务器节点
	 *
	 */
	protected function getDelAction()
	{
		$serverid = intval($_GET['serverid']);
		$rs = $this->db->get_one("SELECT COUNT(*) AS total FROM cms_resource WHERE serverid='$serverid'");
		if ($rs['total'] > 0) {
			Showmsg('此存储节点下已经上传了内容，您不能删除它，只能编辑。');
		}
		$this->db->update("DELETE FROM cms_server WHERE serverid='$serverid'");
		require_once(R_P . 'require/class_cache.php');
		Cache::writeCache('server');
		adminmsg('成功删除该存储点');
	}
	
	/**
	 * 在URL后面统一加上斜杠
	 *
	 * @param string $url
	 * @return string
	 */
	protected function checkURL($url)
	{
		if (strrpos($url, '/') !== (strlen($url) - 1))
		{
			return $url . '/';
		}
		else 
		{
			return $url;
		}
	}
	
	/**
	 * 判断路径跟域名是否匹配
	 *
	 * @param string $path
	 * @param string $url
	 * @return boolean
	 */
	protected function checkPath($path, $url)
	{
		$path = stripslashes($path);
		if (!is_writeable($path))
		{
			Showmsg($path . '目录不可写。');
		}
		if (strpos($url, 'http://localhost') === 0)
		{
			return true; //本地的就不做测试
		}
		
		writeover($path . 'system.txt', 'system test');
		$string = file_get_contents($url . 'system.txt');
		if ($string != 'system test')
		{
			//Showmsg('您所提供的路径和域名不匹配，无法通过该域名访问到该路径');
			//return false;
		}

		if (!unlink($path . 'system.txt'))
		{
			Showmsg('该路径下无法删除文件，请检查权限');
		}
		return true;
	}

}

new Server();
?>